'use strict';
const Controller = require('egg').Controller;
const axios = require('axios');

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  /* 登陆get请求 */
  async login() {
    const { ctx } = this;
    const { code } = ctx.request.query;
    const { appid, secret } = ctx.app.config.jwt;
    await axios.get(`https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`).then(res => {
      ctx.app.config.jwt.session_key = res.data.session_key;
      ctx.app.config.jwt.openid = res.data.openid;
    });
    const token = ctx.app.jwt.sign({ openid: ctx.app.config.jwt.openid }, ctx.app.config.jwt.session_key, { expiresIn: '1d' });
    // console.log(token, '---token');

    ctx.body = {
      token,
      code: 200,
    };
  }
}

module.exports = HomeController;
